﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;//chạy nha

namespace QuanLyKho
{
    public partial class frm_Qlnv : Form
    {
        ConnectDB Db = new ConnectDB();
        DataGridViewRow row_dshh = new DataGridViewRow();
        public frm_Qlnv()
        {
            InitializeComponent();
        }

        public void showDSNhanVien()
        {
            string sql = "select nv.MaNV, nv.TenNV,nv.DienThoai,nv.DiaChi,cv.TenCV from NhanVien nv, ChucVu cv where nv.MaCV=cv.MaCV and nv.IsDeleted = 1";
            DataView dv = new DataView(Db.getDataTable(sql));


            dgv_Nhanvien.DataSource = dv;

            dgv_Nhanvien.ClearSelection();
        }

        /// <summary>
        /// //////////////////////
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtmnv.Text = dgv_Nhanvien.CurrentRow.Cells["manv"].Value.ToString();
            txthoten.Text = dgv_Nhanvien.CurrentRow.Cells["tennv"].Value.ToString();
            txtdienthoai.Text = dgv_Nhanvien.CurrentRow.Cells["dienthoai"].Value.ToString();
            txtdiachi.Text = dgv_Nhanvien.CurrentRow.Cells["diachi"].Value.ToString();
            cb_chucvu.Text = dgv_Nhanvien.CurrentRow.Cells["tencv"].Value.ToString();
            
        }

        private void frm_Qlnv_Load(object sender, EventArgs e)
        {
            Load_Tenchucvu();
            showDSNhanVien();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //string manv = txtmnv.Text.Trim();//
            string tennv = txthoten.Text.Trim();
            string dienthoai = txtdienthoai.Text.Trim();
            string diachi = txtdiachi.Text.Trim();
            string tenchucvu = cb_chucvu.SelectedValue.ToString();

            if (tennv != "" && dienthoai != "" && diachi != "" && tenchucvu != "")// này làm gì
            {
                string manvnew = Db.GetLastIDNumber("NhanVien", "MaNV", "NV0");
                //MessageBox.Show(manvnew);
                manvnew = manvnew.Substring(3);
                int temp = int.Parse(manvnew);
                manvnew = "NV0" + (temp + 1).ToString();

                if (kiemtr_sv(manvnew) == true)
                {

                    DialogResult result = MessageBox.Show("Bạn có thực sự muốn tao moi nhan vien: " + manvnew + "-" + tennv + " ?", "Them nhan vien", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        string sql = "INSERT INTO NhanVien " +
                               " VALUES (N'" + manvnew + "', N'" + tennv + "', " + dienthoai + ", N'" + diachi + "', N'" + tenchucvu + "', 1)";
                        Db.ExecuteNonQuery(sql);
                        MessageBox.Show("Nhân viên: '" + tennv + "' được thêm thành công !!!");
                        showDSNhanVien();
                    }
                    
                }
                else
                    MessageBox.Show("Trùng mã nhân viên, vui lòng kiểm tra lại");
            }
            else
                MessageBox.Show("Vui long dien day du thong tin");

            

                
        }
        public void Load_Tenchucvu()
        {
            string sql = "SELECT * FROM ChucVu";
            DataView dv = new DataView(Db.getDataTable(sql));
            cb_chucvu.DataSource=dv;
            cb_chucvu.DisplayMember = "Tencv";
            cb_chucvu.ValueMember = "Macv";
        }

        private Boolean kiemtr_sv(string mnv)
        {

            
            //kết nối

            //Đọc dữ liệu lên để kiểm tra
            string sqlkt = "select * from NhanVien where MaNV = N'" + mnv + "'"; //viet z code mai vong while lap 1 lan la bjk ket qua
            // câu lệnh proc sqlkt trong database
            
            
            SqlDataReader dr;
            dr = Db.getDataReader(sqlkt);
            Boolean kt = true;
            while (dr.Read())
            {
                if (mnv == dr[0].ToString())
                {
                    kt = false;
                    break;
                }
            }

            Db.disconnect();
   
            return kt;
        }

        private void dgv_Nhanvien_SelectionChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(dgv_Nhanvien.SelectedRows.Count.ToString());
            //foreach (DataGridViewRow row in dgv_Nhanvien.SelectedRows)
            //{
            //    txtmnv.Text = dgv_Nhanvien.CurrentRow.Cells["manv"].Value.ToString();
            //    txthoten.Text = dgv_Nhanvien.CurrentRow.Cells["tennv"].Value.ToString();
            //    txtdienthoai.Text = dgv_Nhanvien.CurrentRow.Cells["dienthoai"].Value.ToString();
            //    txtdiachi.Text = dgv_Nhanvien.CurrentRow.Cells["diachi"].Value.ToString();
            //    cb_chucvu.Text = dgv_Nhanvien.CurrentRow.Cells["tencv"].Value.ToString();
            //}
            
        }

        private void dgv_Nhanvien_CellClick(object sender, DataGridViewCellEventArgs e)//su kien cua no khoi tao
        {
            //MessageBox.Show("cell click");
            int rowIndex = e.RowIndex;
            //MessageBox.Show(rowIndex.ToString());
            if (rowIndex >= 0)
            {
                DataGridViewRow row = dgv_Nhanvien.Rows[rowIndex];
                //string value1 = row.Cells[1].Value.ToString();
                //MessageBox.Show(value1);
                txtmnv.Text = dgv_Nhanvien.CurrentRow.Cells["manv"].Value.ToString();
                txthoten.Text = dgv_Nhanvien.CurrentRow.Cells["tennv"].Value.ToString();
                txtdienthoai.Text = dgv_Nhanvien.CurrentRow.Cells["dienthoai"].Value.ToString();
                txtdiachi.Text = dgv_Nhanvien.CurrentRow.Cells["diachi"].Value.ToString();
                cb_chucvu.Text = dgv_Nhanvien.CurrentRow.Cells["tencv"].Value.ToString();

            }
        }

        private void txtdienthoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtmnv.Visible=true;
            string manv = txtmnv.Text;
            string tennv = txthoten.Text;
            string dienthoai = txtdienthoai.Text;
            string diachi = txtdiachi.Text;
            string chucvu = cb_chucvu.SelectedValue.ToString();           

            string sql = " UPDATE NhanVien " +
                " SET manv =  N'" + manv + "' , tennv = N'" + tennv + "' , dienthoai = N'" + dienthoai + "', diachi = N'" + diachi + "' ,macv  = N'" + chucvu + "'  WHERE manv = N'" + manv + "'";

            Db.ExecuteNonQuery(sql);
            MessageBox.Show("Update thành công !!!");
            showDSNhanVien();
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string manv = txtmnv.Text;

            DialogResult result = MessageBox.Show("Bạn có thực sự muốn xóa nhan vien: " + manv + "?", "Xóa nhan vien", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                string sql = " UPDATE NhanVien " +
               " SET IsDeleted = 0" +
               " WHERE MaNV= N'" + manv + "'";

                Db.ExecuteNonQuery(sql);
                MessageBox.Show("Xóa thành công !!!");
                showDSNhanVien();
            }
            else if (result == DialogResult.No)
            {
                //...
            }
            
        }

    }
}
